{-
Arquivo onde eu fiz meus testes. Uma bagunça, minha mesa de experimentos. Olhe por conta e risco. Certas funções comentadas não compilam mais (a maioria porque eu substitui diffCode por diffCode')
-}

import SourceData
import Data.Tree
import Control.Monad
import Test.QuickCheck
import SchemeParser

{-
main = do
  putStrLn $ show $ diffCode sSource1 sSource2
  return ()
-}

sSource1 = parseScheme "(+ 1 q (3 a))"
sSource2 = parseScheme "(+ (3 . 5) 4 (f a))"

a = Node {
      rootLabel = (Token 0 "foo" "function"),
      subForest = [
       Node {
         rootLabel = (Token 1 "int" "type"),
         subForest = []},
       Node {
         rootLabel = (Token 2 "bar" "identifier"),
         subForest = []}
       ,Node {
         rootLabel = (Token 3 "baz" "identifier"),
         subForest = []}

    ]}

b = Node {
      rootLabel = (Token 0 "foo" "function"),
      subForest = [
       Node {
         rootLabel = (Token 1 "double" "type"),
         subForest = []},
       Node {
         rootLabel = (Token 2 "bar" "identifier"),
         subForest = []}
--       ,Node {
  --       rootLabel = (Token 3 "baz" "identifier"),
    --     subForest = []}
    ]}

c = Node 1 [(Node 1 []), (Node 3 [])]
d = Node 1 [(Node 4 []), (Node 3 [])]

f = Node "f" []
g = Node "g" []
fg = Node "f" [g, f]



-- q = fmap Code (diffCode a b zeroSource)
--q = mapM_ (putStrLn . show) (flatten $ (fmap Code (diffCode a b zeroSource)))
--test = q == (Node {rootLabel = Right (Token {lineNumber = 0, sourceCode = "foo", tokenType = "function"}), subForest = [Node {rootLabel = Left (Token {lineNumber = 1, sourceCode = "int", tokenType = "type"},Token {lineNumber = 1, sourceCode = "double", tokenType = "type"}), subForest = []},Node {rootLabel = Right (Token {lineNumber = 2, sourceCode = "bar", tokenType = "identifier"}), subForest = []}]})

--w = subsOneNode fg (== "f") "g"

--e = patchTree a (diffCode a b zeroSource)


prop_isPatching :: Source -> Source -> Patch -> Property
prop_isPatching old new patch = sameTreeStructure old new ==>
    (patchTree old (diffCode' old new)) == new


                
instance Differ Int where
    a =/= b = undefined


--instance Arbitrary Char where
--    arbitrary = elements (['A'..'Z'] ++ ['a' .. 'z'] ++ " ~!@#$%^&*()")

instance Arbitrary Token where
    arbitrary = oneof [
                 liftM3 Token arbitrary arbitrary arbitrary]

instance (Arbitrary a => Arbitrary (Tree a)) where
    arbitrary = oneof [
                 liftM2 Node arbitrary arbitrary
                ,liftM2 Node arbitrary (return [])]

sameTreeStructure :: Tree a -> Tree b -> Bool
sameTreeStructure (Node _ []) (Node _ []) = True
sameTreeStructure (Node _ _) (Node _ []) = False
sameTreeStructure (Node _ []) (Node _ _) = False
sameTreeStructure (Node _ as) (Node _ bs) = (and (zipWith sameTreeStructure as bs)) && ((length as) == (length bs))

main = quickCheck prop_isPatching

